package demo.web;



import demo.exception.BizAssert;
import demo.util.JsonUtils;

import javax.servlet.http.HttpServletResponse;

/**
 * @author meilin.huang
 * @date 2021-05-12 3:09 下午
 */
public class WebUtils {

    /**
     * 响应json对象
     *
     * @param response response
     * @param obj      响应对象
     */
    public static void writeJson(HttpServletResponse response, Object obj) {
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("utf-8");
        try {
            response.getWriter().write(JsonUtils.toJSONString(obj));
            response.flushBuffer();
        } catch (Exception e) {
            throw BizAssert.newException("响应json异常: %s", e);
        }
    }
}
